/*
 * @Author: ZhanXie
 * @Date: 2020-08-11 11:09:40
 * @LastEditors: ZhanXie
 * @LastEditTime: 2020-08-11 12:22:05
 * @Description: sequelize 实例
 * @FilePath: /weibo/src/db/seq.js
 */

const Sequelize = require('sequelize')
const { MYSQL_CONF } = require('../conf/db')
const { isProd, isTest } = require('../utils/env')

const { host, user, password, database } = MYSQL_CONF

const conf = {
  host,
  dialect: 'mysql'
}

if (isTest) {
  // 测试环境不打印 sql 日志
  conf.logging = f => f
}

if (isProd) {
  // 线上环境使用连接池
  conf.pool = {
    max: 5, // 连接池中最大的连接数量
    min: 0, // 最小
    idle: 100000 // 如果一个连接池 10s 之内没有被使用，则释放
  }
}

const seq = new Sequelize(database, user, password, conf)

module.exports = seq